package
{

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.ui.*;
	import flash.utils.getTimer;

	public class TestEmbedFont extends Sprite
	{
		[Embed( source="./a2.png" )]
		private var iconClass  :  Class;
		private var iconImg  :  Bitmap;

		public function TestEmbedFont()  :  void
		{
			this.iconImg  =  new iconClass() as Bitmap;

			if( stage )
			{
				this.init();
			}
			else
			{
				this.addEventListener( Event.ADDED_TO_STAGE, init );
			}

			var m  :  int  =  gcf( gcf( gcf( 3, 5 ), 7 ), 9 );

			var n  :  int;

			for( var k  :  int  =  1; ; k++ )
			{
				if(( m * k - 1 ) % 11 == 0 )
				{
					n  =  m * k - 1;
					break;
				}
			}
			trace( n );

//			for( var i  :  int  =  0; ; i++ )
//			{
//				var num  :  Number  =  315 * i - 1;
//
//				if( num % 11 == 0 )
//				{
//					trace( "i = " + i, "num = " + num );
//				}
//
//				if( num > 10000 )
//				{
//					break;
//				}
//			}

			prettyCode();
		}

		/**
		 *
		 * 记录一些漂亮的代码
		 *
		 * */
		public function prettyCode()  :  void
		{
			//取反=======================================================================
			var i  :  int           =  -28;
			i  =  -i;
			trace( i );
			//相当于：
			i  =  ~i + 1;
			trace( i );
			//或者
			i  =  ( i ^ -1 ) + 1;
			trace( i );

			//忘记 Math.abs()吧 (Forget Math.abs() for time critical code.) =============
			//version 1 比 Math.abs() 快了2500% ，version 2 居然比 version 1 又快了20% ！
			var x  :  int           =  -1029
			//version 1
			x  =  x < 0 ? -x : x;
			trace( x );
			//version 2
			x  =  ( x ^ ( x >> 31 )) - ( x >> 31 );
			//Number 到 integer(整数)转换=================================================
			//=在AS3中使用int(x)快了600% 。尽管如此位操作版本在AS2中工作的更好
			x  =  int( 1.232 )
			trace( x );
			//相当于：
			x  =  1.232 >> 0;
			trace( x );
			//===========================================================================
			//使用bitwise AND快速取模 （Fast modulo operation using bitwise AND）
			//如果除数是2的次方，取模操作可以这样做：模数= 分子 & (除数 – 1)
			x  =  131 % 4;
			//相当于：
			x  =  131 & ( 4 - 1 );
			trace( "取余131 % 4 = " + x );
			//检查是否为偶数（Check if an integer is even/uneven using bitwise AND）======
			//这里快了 600% 
			var isEven  :  Boolean  =  ( i % 2 ) == 0;
			//相当于：
			isEven  =  ( i & 1 ) == 0;

			//===========================================================================
			//使用异或运算交换整数而不需要用临时变量
			//很可爱的技巧, 在本页顶端的链接里有详细的解释 ，这里快了 20%
			var a  :  int           =  6;
			var b  :  int           =  10;
			var t  :  int           =  a;
			a  =  b;
			b  =  t;
			//相当于:
			a  ^=  b;
			b  ^=  a;
			a  ^=  b;
			trace( "a = " + a, " b = " + b );
			a  =  b | (( b  =  a ) & 0 );
			trace( "a = " + a, " b = " + b );
			test();
		}

		//测试两个变更变换
		public function test()  :  void
		{
			var arr  :  Array        =  [];

			for( var i  :  int  =  0; i < 10000000; i++ )
			{
				arr[ i ]  =  i;
			}

			var curTime  :  Number   =  getTimer();

			for( i  =  0; i < 999999; i  +=  2 )
			{
				var k  :  int  =  i + 1;
				arr[ i ]  =  arr[ k ] | (( arr[ k ]  =  arr[ i ]) & 0 );

			}

			var lastTime  :  Number  =  getTimer();
			var costTime  :  Number  =  lastTime - curTime;
			trace( arr[ 0 ], arr[ 1 ], arr[ 2 ], arr[ 3 ]);
			trace( " costTime = " + costTime );
			//trace( arr );
			curTime  =  getTimer();

			for( i  =  0; i < 999999; i  +=  2 )
			{
				k  =  i + 1;
				arr[ i ]  ^=  arr[ k ];
				arr[ k ]  ^=  arr[ i ];
				arr[ i ]  ^=  arr[ k ];
			}
			lastTime  =  getTimer();
			costTime  =  lastTime - curTime;
			trace( arr[ 0 ], arr[ 1 ], arr[ 2 ], arr[ 3 ]);
			trace( " costTime = " + costTime );
			//trace( arr );
		}

		// 最大公约数
		public function gcd( a  :  int, b  :  int )  :  int
		{
			return b == 0 ? a : gcd( b, a % b );
		}

		// 最大公倍数   
		public function gcf( a  :  int, b  :  int )  :  int
		{
			return a * b / gcd( a, b );
		}

		/**
		 * 清空数组但不改变其地址
		 *
		 * @param arr 已分配内存地址的数组，不能为NULL
		 *
		 */
		public function clearArr( arr  :  Array )  :  void
		{
			if( !arr || arr.length == 0 )
				return;

			arr.splice( 0 ); //arr.splice(0, arr.length);
			return;
		}

		protected function init( e  :  Event  =  null )  :  void
		{
			var textfield  :  TextField  =  new TextField();
			textfield.text  =  "";
			textfield.x  =  this.stage.stageWidth / 2;
			textfield.y  =  this.stage.stageHeight / 2;
			this.addChild( textfield );

			if( Mouse.supportsNativeCursor )
			{
				var mouseImage  :  Vector.<BitmapData>   =  new Vector.<BitmapData>();
				mouseImage.push( this.iconImg.bitmapData );
				var bitmapData  :  BitmapData            =  new BitmapData( 32, 32, true, 0 );
				var shape  :  Shape                      =  new Shape();
				shape.graphics.beginFill( 0xFF0000, .85 );
				shape.graphics.drawRect( 0, 0, 32, 32 );
				shape.graphics.endFill();
				bitmapData.draw( shape );
				//mouseImage.push(bitmapData);
				var mouseCursorData  :  MouseCursorData  =  new MouseCursorData();
				mouseCursorData.frameRate  =  1;
				mouseCursorData.data  =  mouseImage;
				Mouse.registerCursor( 'testCursor', mouseCursorData );
				Mouse.cursor  =  'testCursor';
				textfield.text  =  '支持本地话的鼠标！';
			}
			else
			{
				textfield.text  =  '不支持本地化的鼠标！';
			}
		}
	}
}